<template>
	<view class="earnings-record full box relative bg-primary flex-col">
		<view class="header-wrap">
			<view class="data-item">
				<text class="data-value">{{ countState.daitixian_price || 0}}元</text>
				<text class="data-label">可提现</text>
			</view>
			<view class="data-item">
				<text class="data-value">{{ countState.tixianzhong_price || 0}}元</text>
				<text class="data-label">提现中的</text>
			</view>
			<view class="data-item">
				<text class="data-value">{{ countState.yitixian_price || 0}}元</text>
				<text class="data-label">已提现</text>
			</view>
		</view>
		<view class="tab-wrap">
			<u-tabs active-color="#3361F0" :bar-height="6" :bold="false" :list="tabList" :is-scroll="false"
				v-model="currentTab" @change="handleTabChange" :bar-style="{bottom:'-4px'}" />
		</view>
		<view class="w-wrap">
			<view class="w-inner">
				<mescroll-uni @init="mescrollInit" :fixed="false" @down="downCallback" @up="upCallback">
					<view class="record-list">
						<template v-if="currentTab < 2">
							<view class="record-item" v-for="(item,index) in list" :key="index">
								<view class="item-header">
									<!-- <image class="item-image" mode="aspectFill"></image> -->
									<view class="item-info">
										<text class="item-name">激励奖励</text>
										<text class="item-date">{{ item.create_time }}</text>
									</view>
									<view class="item-price">
										<text class="price-value">￥{{ item.fenxiao_jiangli_price}}</text>
										<text class="price-label">{{ item.status_text }}</text>
									</view>
									<view class="item-checkbox" :class="[item.checked ? 'active' : '']"
										v-if="currentTab === 0" @click="handleToggleItem(item)">
										<u-icon name="checkmark" color="#fff" size="28rpx" />
									</view>
								</view>
								<view class="item-remark">
									<text>备注:{{ item.content || ""}}</text>
								</view>
							</view>
						</template>
						<template v-else>
							<view class="earnings-item" v-for="(item,index) in list" :key="index">
								<view class="earnings-header">
									<text class="is-date">{{ item.month }}</text>
									<text class="is-price">￥{{ item.month_total_price }}</text>
								</view>
								<view class="detail-list" v-if="item.data && item.data.length">
									<view class="detail-header">
										<text class="header-title">详细记录</text>
										<u-icon @click="handleToggleItem(item)" class="arrow-active" name="arrow-down"
											size="28rpx" color="#898989" :class="[item.checked ? 'active' : '']" />
									</view>
									<template v-if="item.checked">
										<view class="detail-record" v-for="(record,recordIndex) in item.data"
											:key="record.id">
											<view class="detail-record-item">
												<text>{{ record.content }}</text>
												<text>金额:￥{{ record.fenxiao_jiangli_price }}</text>
											</view>
											<view class="detail-record-item">
												<text>水管家:{{ record.user.nickname }}</text>
												<text>{{ record.create_time }}</text>
											</view>
										</view>
									</template>
								</view>
							</view>
						</template>
					</view>
				</mescroll-uni>
			</view>
		</view>
		<view class="page-footer" v-if="currentTab === 0">
			<view class="checkall-wrap">
				<view class="item-checkbox" :class="[isCheckAll ? 'active' : '']" @click="handleCheckAll()">
					<u-icon name="checkmark" color="#fff" size="28rpx" />
				</view>
				<text class="check-text">全选</text>
			</view>
			<view class="total-price">
				<text class="heji-text">合计：</text>
				<text class="heji-value">￥{{ totalPrice }}</text>
			</view>
			<u-button type="primary" shape="circle" @click="handleConfirm">确认提现</u-button>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import { request, toast, showLoading } from "@/utils/tool";
	import currency from "currency.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				currentTab: 0,
				list: [],
				countState: {
					daitixian_price: 0,
					tixianzhong_price: 0,
					yitixian_price: 0
				},
				tabList: [{
					name: '待提现收益',
				}, {
					name: '历史收益'
				}, {
					name: '合伙人管理收益'
				}]
			}
		},
		computed: {
			totalPrice() {
				if (this.currentTab !== 0) return ""
				return this.list.reduce((pre, cur) => {
					const value = cur.checked ? cur.fenxiao_jiangli_price : 0
					return currency(pre).add(value).value
				}, 0)
			},
			isCheckAll: {
				get() {
					return this.list.length > 0 && this.list.every(item => item.checked)
				},
				set(v) {
					this.list.forEach(item => {
						item.checked = v
					})
				},
			}
		},
		onLoad() {
			this.getCountState()
		},
		methods: {
			async handleConfirm() {
				const ids = this.list.filter(item => item.checked).map(item => item.id)
				if (!ids.length) return toast({ title: "请选择" })
				const hide = showLoading("正在提现")
				const resp = await request({
					url: "/beihuotihuoorder/tixian",
					data: {
						kucun_tihuo_order_fenxiao_ids: ids.toString()
					}
				}).finally(hide)
				if (resp.code !== 1) return toast({ title: resp.msg || "申请失败" })
				toast({ title: "申请成功" })
				this.mescroll.triggerDownScroll()
				this.getCountState()
			},
			handleCheckAll() {
				this.isCheckAll = !this.isCheckAll
			},
			handleToggleItem(rowItem) {
				rowItem.checked = !rowItem.checked
			},
			async getManageList(page, limit) {
				const resp = await request({
					url: '/beihuotihuoorder/zhaoshangfenxiaolists',
					method: "GET",
					data: {
						page_no: page,
						page_size: limit
					}
				})
				if (page === 1) this.list = []
				const { lists, count } = resp.data
				this.list.push(...lists.map(item => {
					item.checked = false
					return item
				}))
				this.mescroll.endBySize(lists.length, count)
			},
			async initData(page, limit) {
				const typeEnum = { 0: 1, 1: 2 }
				const resp = await request({
					url: "/beihuotihuoorder/kucuntihuofenxiaolists",
					method: "GET",
					data: {
						page_no: page,
						page_size: limit,
						type: typeEnum[this.type] ?? 1
					}
				})
				if (page === 1) this.list = []
				const { lists, count } = resp.data
				console.log(resp, 'resp');
				this.list.push(...lists.map(item => {
					item.checked = false
					return item
				}))
				this.mescroll.endBySize(lists.length, count)
			},
			upCallback(ev) {
				console.log('upCallback');
				const { num, size } = ev
				if (this.currentTab < 2) {
					return this.initData(num, size)
				}
				this.getManageList(num, size)
			},
			handleTabChange(index) {
				this.currentTab = index
				this.mescroll.triggerDownScroll()
			},
			async getCountState() {
				const resp = await request({
					url: '/beihuotihuoorder/fenxiaoindex',
					method: "GET",
				})
				this.countState = resp.data || {}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.earnings-record {
		.page-footer {
			padding: 16rpx 32rpx;
			background-color: #FFFFFF;
			display: flex;
			align-items: center;

			.total-price {
				margin-left: auto;
				margin-right: 32rpx;

				.heji-text {
					font-size: 24rpx;
					color: #1D2129;
					line-height: 40rpx;
				}

				.heji-value {
					font-weight: bold;
					font-size: 40rpx;
					color: #F53F3F;
				}
			}

			.checkall-wrap {
				display: flex;
				align-items: center;

				.item-checkbox {
					margin-left: 10rpx;
					width: 41rpx;
					height: 41rpx;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					border: 2rpx solid #D9D9D9;
					background-color: #FFFFFF;

					&.active {
						background-color: #3361F0;
						border-color: #3361F0;
					}
				}

				.check-text {
					font-size: 24rpx;
					color: #86909C;
					line-height: 40rpx;
					margin-left: 8rpx;
				}
			}
		}

		.record-list {
			padding: 24rpx 18rpx 0;
			box-sizing: border-box;
			position: relative;

			.earnings-item {
				padding: 24rpx 20rpx;
				box-sizing: border-box;
				position: relative;
				background: #FFFFFF;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-bottom: 24rpx;

				.detail-list {
					margin-top: 10rpx;
					padding: 12rpx 16rpx;
					background: #EEEEEE;
					border-radius: 12rpx 12rpx 12rpx 12rpx;

					.detail-record {
						padding-top: 12rpx;
						border-top: 2rpx solid #E2E2E2;

						.detail-record-item {
							display: flex;
							align-items: center;
							justify-content: space-between;
							font-size: 24rpx;
							color: #5F5F5F;
							line-height: 44rpx;
						}
					}

					.detail-header {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.arrow-active {
							transform: rotate(180deg);

							&.active {
								transform: rotate(0);
							}
						}

						.header-title {
							font-size: 24rpx;
							color: #535353;
							line-height: 44rpx;
						}
					}
				}

				.earnings-header {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.is-date {
						font-size: 28rpx;
						color: #535353;
						line-height: 44rpx;
					}

					.is-price {
						font-weight: bold;
						font-size: 28rpx;
						color: #FF0000;
						line-height: 44rpx;
					}
				}

				&:last-child {
					margin-bottom: 0;
				}
			}

			.record-item {
				padding: 24rpx 18rpx;
				box-sizing: border-box;
				position: relative;
				background: #FFFFFF;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-bottom: 24rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.item-remark {
					padding: 10rpx 18rpx;
					margin-top: 4rpx;
					font-size: 26rpx;
					color: #535353;
					line-height: 44rpx;
					background: #EEEEEE;
					border-radius: 12rpx 12rpx 12rpx 12rpx;
					word-break: break-all;
					white-space: pre-wrap;
				}

				.item-header {
					display: flex;
					align-items: center;

					.item-checkbox {
						margin-left: 10rpx;
						width: 41rpx;
						height: 41rpx;
						border-radius: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
						border: 2rpx solid #D9D9D9;
						background-color: #FFFFFF;

						&.active {
							background-color: #3361F0;
							border-color: #3361F0;
						}
					}

					.item-price {
						margin-left: auto;
						display: flex;
						flex-flow: column;
						align-items: flex-end;

						.price-value {
							font-weight: bold;
							font-size: 28rpx;
							color: #FF0000;
							line-height: 44rpx;
						}

						.price-label {
							font-size: 24rpx;
							color: #535353;
							line-height: 38rpx;
						}
					}

					.item-info {
						margin-left: 8rpx;
						display: flex;
						flex-flow: column;

						.item-name {
							font-size: 28rpx;
							color: #535353;
							line-height: 44rpx;
						}

						.item-date {
							font-size: 24rpx;
							color: #535353;
							line-height: 40rpx;
						}
					}

					.item-image {
						width: 68rpx;
						height: 70rpx;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
					}
				}
			}
		}

		.tab-wrap {
			background-color: #FFFFFF;
			border-bottom: 1rpx solid #E7E7E7;
		}

		.header-wrap {
			display: flex;
			align-items: center;
			position: relative;
			padding: 24rpx 0;
			box-sizing: border-box;
			background: #FFFFFF;
			box-shadow: 0rpx 8rpx 8rpx 0rpx rgba(0, 0, 0, 0.04);

			.data-item {
				display: flex;
				flex-flow: column;
				align-items: center;
				flex: 1;
				border-right: 2rpx solid #E3E3E3;

				&:last-child {
					border-right: 0;
				}

				.data-label {
					font-size: 24rpx;
					color: #606060;
					line-height: 36rpx;
					margin-top: 8rpx;
				}

				.data-value {
					font-weight: bold;
					font-size: 30rpx;
					color: #606060;
					letter-spacing: 1px;
					line-height: 42rpx;
				}
			}
		}
	}
</style>